﻿using Engine;
using Engine.Graphics;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class ComponentSlowDown : Component, IUpdateable
    {
        public BuffParticleSystem onBuffParticleSystem;

        public float Slown_first;

        public SubsystemGameInfo m_subsystemGameInfo;

        public SubsystemTerrain m_subsystemTerrain;

        public SubsystemTime m_subsystemTime;

        public SubsystemAudio m_subsystemAudio;

        public SubsystemParticles m_subsystemParticles;

        public ComponentPlayer m_componentPlayer;

        public ComponentCreature m_componentCreature;

        public float m_SlowRate;

        public float m_SlowDuration;

        public Random m_random = new Random();

        public float SlowRate => m_SlowRate;

        public bool IsActive => m_SlowDuration > 0f;

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public void StartBuff(float time, float rate)
        {
            m_SlowRate = rate;
            m_SlowDuration = time;
        }

        public void StopBuff()
        {
            if (m_componentCreature != null)
            {
                m_SlowDuration = 0f;
                if ((double)Slown_first > 3.7)
                {
                    Slown_first = 3.9f;
                }

                m_componentCreature.ComponentLocomotion.WalkSpeed = Slown_first;
                Slown_first = 0f;
                if (m_componentPlayer != null)
                {
                    m_componentPlayer.ComponentGui.DisplaySmallMessage("迟缓效果结束！", Color.White, blinking: true, playNotificationSound: false);
                }
            }
        }

        public void Update(float dt)
        {
            if (!IsActive || !(m_SlowDuration > 0f) || !(m_SlowRate > 0f))
            {
                return;
            }

            if (Slown_first == 0f)
            {
                Slown_first = m_componentCreature.ComponentLocomotion.WalkSpeed;
            }

            m_componentCreature.ComponentLocomotion.WalkSpeed = 1f;
            if (onBuffParticleSystem == null)
            {
                onBuffParticleSystem = new BuffParticleSystem
                {
                    Texture = ContentManager.Get<Texture2D>("Textures/Star")
                };
                m_subsystemParticles.AddParticleSystem(onBuffParticleSystem);
            }

            BoundingBox boundingBox = m_componentCreature.ComponentBody.BoundingBox;
            onBuffParticleSystem.Position = 0.5f * (boundingBox.Min + boundingBox.Max);
            onBuffParticleSystem.Radius = 0.5f * MathUtils.Min(boundingBox.Max.X - boundingBox.Min.X, boundingBox.Max.Z - boundingBox.Min.Z);
            onBuffParticleSystem.Color = new Color(60f, 60f, 60f);
            onBuffParticleSystem.Size = m_componentCreature.ComponentBody.BoxSize.XZ * 0.1f;
            m_SlowDuration -= dt;
            if (m_SlowDuration <= 0f)
            {
                if (onBuffParticleSystem != null)
                {
                    onBuffParticleSystem.IsStopped = true;
                    onBuffParticleSystem = null;
                }

                StopBuff();
            }
        }

        public override void OnEntityRemoved()
        {
            if (onBuffParticleSystem != null)
            {
                onBuffParticleSystem.IsStopped = true;
                onBuffParticleSystem = null;
            }
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            m_subsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
            m_subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
            m_subsystemTime = base.Project.FindSubsystem<SubsystemTime>(throwOnError: true);
            m_subsystemAudio = base.Project.FindSubsystem<SubsystemAudio>(throwOnError: true);
            m_subsystemParticles = base.Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
            m_componentCreature = base.Entity.FindComponent<ComponentCreature>(throwOnError: true);
            m_componentPlayer = m_componentCreature.Entity.FindComponent<ComponentPlayer>();
            m_SlowDuration = valuesDictionary.GetValue("SlowDuration", 0f);
            m_SlowRate = valuesDictionary.GetValue("SlowRate", 1f);
            Slown_first = valuesDictionary.GetValue("SlowFirst", 0f);
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            valuesDictionary.SetValue("SlowDuration", m_SlowDuration);
            valuesDictionary.SetValue("SlowRate", m_SlowRate);
            valuesDictionary.SetValue("SlowFirst", Slown_first);
        }
    }
}